// 判断值是否为某个类型
export const is = (val: unknown, type: string) => {
  return toString.call(val) === `[object ${type}]`
}

// 是否为函数
export const isFunction = <T = Function>(val: unknown): val is T => {
  return is(val, 'Function')
}

// 是否为字符串
export const isString = (val: unknown): val is string => {
  return is(val, 'String')
}

export function isNumber(val: unknown): val is number {
  return is(val, 'Number')
}

export const isObject = (val: any): val is Record<any, any> => {
  return val !== null && is(val, 'Object')
}

// 判断路径是不是带协议的外链
export const isExternal = (path: string): boolean => {
  return /^(https?:|mailto:|tel:)/.test(path)
}

export const isServer = typeof window === 'undefined'

export const isDef = <T = unknown>(val?: T): val is T => {
  return typeof val !== 'undefined'
}

export const isUnDef = <T = unknown>(val?: T): val is T => {
  return !isDef(val)
}
